<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="图书" prop="bookName">
        <el-input
          v-model="queryParams.bookName"
          placeholder="请输入图书"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="核销码" prop="hxCode">
        <el-input
          v-model="queryParams.hxCode"
          placeholder="请输入核销码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="提交时间">
        <el-date-picker
          v-model="dateRange"
          style="width: 240px;"
          value-format="yyyy-MM-dd"
          type="daterange"
          rang-separator="到"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="审核状态" prop="checkStatus" v-if="type=='2'">
        <el-select v-model="queryParams.checkStatus" placeholder="请选择审核状态" clearable>
          <el-option
            v-for="dict in dict.type.check_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="primary"-->
      <!--          plain-->
      <!--          icon="el-icon-plus"-->
      <!--          size="mini"-->
      <!--          @click="handleAdd"-->
      <!--          v-hasPermi="['system:sq:add']"-->
      <!--        >新增-->
      <!--        </el-button>-->
      <!--      </el-col>-->
      <el-col :span="1.5">
        <el-button v-if="type=='2'"
                   type="primary"
                   plain
                   size="mini"
                   @click="checkPass(null,2)"
        >审核通过
        </el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button v-if="type=='2'"
                   type="danger"
                   plain
                   size="mini"
                   @click="checkPass(null,3)"
        >审核不通过
        </el-button>
      </el-col>
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="success"-->
      <!--          plain-->
      <!--          icon="el-icon-edit"-->
      <!--          size="mini"-->
      <!--          :disabled="single"-->
      <!--          @click="handleUpdate"-->
      <!--          v-hasPermi="['system:sq:edit']"-->
      <!--        >修改</el-button>-->
      <!--      </el-col>-->
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="danger"-->
      <!--          plain-->
      <!--          icon="el-icon-delete"-->
      <!--          size="mini"-->
      <!--          :disabled="multiple"-->
      <!--          @click="handleDelete"-->
      <!--          v-hasPermi="['system:sq:remove']"-->
      <!--        >删除</el-button>-->
      <!--      </el-col>-->

      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="sqList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="编号" align="center" prop="id"/>
      <el-table-column label="图书" align="center" prop="bookName"/>
      <el-table-column label="图书图片" align="center" prop="bookImg">
        <template slot-scope="scope">
          <image-preview :src="scope.row.bookImg" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="核销码" align="center" prop="hxCode"/>
      <el-table-column label="是否核銷" align="center" prop="isHx">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.is_no" :value="scope.row.isHx"/>
        </template>
      </el-table-column>
      <el-table-column label="用户姓名" align="center" prop="nickName"/>
      <el-table-column label="手机号" align="center" prop="phonenumber"/>
      <el-table-column label="提交时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="开始时间" align="center" prop="startDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="归还时间" align="center" prop="returnDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.returnDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="审核状态" align="center" prop="checkStatus" v-if="type=='2'">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.check_status" :value="scope.row.checkStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="审核时间" align="center" prop="checkDate" width="180" v-if="type=='2'">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.checkDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>

      <el-table-column label="审核备注" align="center" prop="checkRemark" v-if="type=='2'"/>
      <!--      <el-table-column label="审核人" align="center" prop="checkUser"/>-->
      <!--      <el-table-column label="是否核销" align="center" prop="isHx">-->
      <!--        <template slot-scope="scope">-->
      <!--          <dict-tag :options="dict.type.is_no" :value="scope.row.isHx"/>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
      <el-table-column label="操作" align="center" fixed="right" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-if="type=='1'"
                     size="mini"
                     type="text"
                     icon="el-icon-edit"
                     @click="handleUpdateHx(scope.row)"
          >核销
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="viewDetail(scope.row)"
          >查看
          </el-button>

          <!--          <el-button-->
          <!--            size="mini"-->
          <!--            type="text"-->
          <!--            icon="el-icon-delete"-->
          <!--            @click="handleDelete(scope.row)"-->
          <!--            v-hasPermi="['system:sq:remove']"-->
          <!--          >删除-->
          <!--          </el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改图书借阅申请对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">

        <!--        <el-form-item label="用户头像" prop="avatar">-->
        <!--          <image-preview :src="form.avatar" :width="50" :height="50"/>-->
        <!--        </el-form-item>-->
        <el-form-item label="用户姓名" prop="nickName">
          <el-input v-model="form.nickName" disabled placeholder="请输入用户姓名"/>
        </el-form-item>
        <el-form-item label="图书" prop="bookName">
          <el-input v-model="form.bookName" placeholder="请输入图书"/>
        </el-form-item>
        <el-form-item label="图书图片" prop="avatar">
          <image-preview :src="form.bookImg" :width="50" :height="50"/>
        </el-form-item>
        <el-form-item label="开始时间" prop="startDate">
          <el-date-picker clearable
                          v-model="form.startDate"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择开始时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="归还时间" prop="returnDate">
          <el-date-picker clearable
                          v-model="form.returnDate"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择归还时间"
          >
          </el-date-picker>
        </el-form-item>
        <!--        <el-form-item label="审核状态" prop="checkStatus">-->
        <!--          <el-radio-group v-model="form.checkStatus">-->
        <!--            <el-radio-->
        <!--              v-for="dict in dict.type.check_status"-->
        <!--              :key="dict.value"-->
        <!--              :label="dict.value"-->
        <!--            >{{ dict.label }}-->
        <!--            </el-radio>-->
        <!--          </el-radio-group>-->
        <!--        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <!--        <el-button type="primary" @click="submitForm">确 定</el-button>-->
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog :title="title" :visible.sync="openCheck" width="800px" append-to-body>
      <el-form ref="checkform" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="审核备注" prop="checkRemark">
          <el-input v-model="checkRemark" type="textarea" :rows="3" placeholder="请输入审核备注"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="checkSubmit(3)">确 定</el-button>
        <el-button @click="cancelCheck">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { listSq, getSq, delSq, addSq, updateSq, checkSq } from '@/api/business/bookBorrowSq'
import { check } from '@/api/business/cardMamage'

export default {
  name: 'BookBorrowSq',
  dicts: ['check_status', 'is_no'],
  props: {
    type: {
      required: true,
      default: '1'
    }
  },

  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      dateRange: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 图书借阅申请表格数据
      sqList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      openCheck: false,
      checkRemark: '',
      checkIdNo: '',
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        bookName: null,
        userId: null,
        startDate: null,
        returnDate: null,
        checkStatus: null,
        checkDate: null,
        checkRemark: null,
        checkUser: null,
        isHx: null,
        hxCode: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询图书借阅申请列表 */
    getList() {
      this.loading = true
      if (this.type == '1') {
        this.queryParams.checkStatus = '2'
      }
      listSq(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.sqList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        bookId: null,
        userId: null,
        startDate: null,
        returnDate: null,
        checkStatus: null,
        checkDate: null,
        checkRemark: null,
        checkUser: null,
        isHx: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        delFlag: null,
        remark: null,
        ids: []

      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = []
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加图书借阅申请'
    },
    checkPass(row, checkStatus) {
      this.reset()
      this.checkIdNo = ''
      if (row) {
        this.form.id = row.id
        this.checkIdNo = row.id
      } else {
        this.form.ids = this.ids
        this.checkIdNo = this.ids
      }
      // 选中的行
      if ('2' == checkStatus) {
        this.checkSubmit('2')
      } else {
        this.checkRemark = ''
        this.title = '审核不通过'
        this.openCheck = true
      }
    },
    checkSubmit(checkStatus) {
      let msg = ''
      if ('2' == checkStatus) {
        msg = '是否确认审核通过编号为'
      } else {
        msg = '是否确认审核不通过编号为'
      }
      const that = this
      // 审核通过
      this.$modal.confirm(msg + this.checkIdNo + '的数据"?').then(function() {
        that.openCheck = false
        that.form.checkRemark = that.checkRemark
        that.form.checkStatus = checkStatus
        that.loading = true
        return checkSq(that.form)
      }).then(() => {
        this.getList()
        that.$modal.msgSuccess('审核成功')
      }).catch(() => {
        that.loading = false
        that.getList()
      })

    },
    cancelCheck() {
      this.openCheck = false
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getSq(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改图书借阅申请'
      })
    },
    handleUpdateHx(row) {
      const id = row.id
      this.form.id = id
      this.form.isHx = '1'
      const that = this
      this.$modal.confirm('是否确认核销？').then(function() {
        return updateSq(that.form)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      })
    },

    viewDetail(row) {
      this.reset()
      const id = row.id || this.ids
      getSq(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = '详情'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateSq(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addSq(this.form).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除图书借阅申请编号为"' + ids + '"的数据项？').then(function() {
        return delSq(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/sq/export', {
        ...this.queryParams
      }, `sq_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
